<template>
	<view>
		<scroll-view
			class="roster-org-container"
			scroll-y
			scroll-with-animation
			refresher-enabled
			:refresher-triggered="isPullRefresh"
			@refresherrefresh="refreshList"
			@scrolltolower="listPageRosterByOrgnation"
			enable-flex
		>
			<view class="top-filter-container">
				<view class="org-info">
					<text class="org-name">{{ orgName }}</text>
					<text class="org-num">{{ countMap.total || 0 }}人</text>
				</view>
				<picker mode="selector" :value="selectIndex" class="state-container" :range="workingStatusList" range-key="value" @change="changeWorkStatus">
					<view class="state">
						<text>{{ workingStatusList[selectIndex].value }}</text>
						<text class="down-arrow"></text>
					</view>
				</picker>
			</view>
			<view :class="'org-list ' + (personList.length == 0 ? 'sp' : '')" v-if="personList.length != 0 || orgnationList.length != 0">
				<view class="project-item-container" v-for="(item, index) in orgnationList" :key="index">
					<view class="projext-item" @tap="toRosterOrg" :data-item="item">
						<commonIcon type="icon-xiangmu" :size="82" v-if="item.type == 2 && item.orgType == 4" />
						<commonIcon type="icon-gongsibanzu" :size="82" v-else />
						<view class="project-item-middle">
							<view class="middle-top">
								<text class="project-type" v-if="item.type == 1">子</text>
								<text class="project-name">{{ item.name }}</text>
							</view>
							<view class="middle-bottom">
								<view class="project-tag" v-for="(tagItem, tagIndex) in item.tags" :key="tagIndex">
									<text class="tag-name">{{ tagItem.key }}</text>

									<text class="tag-num">{{ tagItem.value }}</text>
								</view>
							</view>
						</view>
						<text class="right-arrow"></text>
					</view>
				</view>
				<view class="person-item-container" @tap="toPersonDetail" :data-item="item" v-for="(item, index) in personList" :key="index">
					<view class="person-item">
						<view :class="'icon-container ' + (item.personType != '10' ? 'orange' : 'green')" v-if="!item.avatar && !loading">
							<commonIcon type="icon-a-laowurenyuan1x" :size="82" v-if="item.personType != '10'" />
							<commonIcon type="icon-a-guanlirenyuan1x" :size="82" v-else />
							<view class="black-icon" @tap.stop.prevent="showBlackReason" v-if="item.isBlack" :data-item="item">
								<commonIcon type="icon-heimingdan" :size="32" />
							</view>
						</view>
						<view class="icon-container" v-else-if="item.avatar && !loading">
							<image :src="item.avatarData" mode="aspectFill" class="person-avatar" />
							<view class="black-icon" @tap.stop.prevent="showBlackReason" v-if="item.isBlack" :data-item="item">
								<commonIcon type="icon-heimingdan" :size="32" />
							</view>
						</view>
						<view class="icon-container" v-else></view>
						<view class="person-item-middle">
							<view class="middle-top">
								<text class="person-name">{{ item.name }}</text>
								<text class="work-type" v-if="item.workTypeName || item.personType">{{ item.workTypeNameData }}</text>
							</view>
							<view class="middle-bottom">{{ item.workTeamName }}</view>
						</view>
						<text class="right-arrow"></text>
					</view>
				</view>
			</view>
			<view class="no-data" v-else>
				<no-data></no-data>
			</view>
		</scroll-view>

		<commonDialog :show="showDialog" title="黑名单提示" :cancelBtn="false" :confirmBtn="false" :closeBtn="true" @close="closeDialog">
			<view class="tip-content">
				<view class="tip-item">
					<text class="tip-item-label">姓名：</text>
					<text class="tip-item-content">{{ blacklist.name }}</text>
				</view>
				<view class="tip-item">
					<text class="tip-item-label">拉黑时间：</text>
					<text class="tip-item-content">{{ blacklist.recordDate }}</text>
				</view>
				<view class="tip-item">
					<text class="tip-item-label">拉黑原因：</text>
					<text class="tip-item-content">{{ blacklist.reason }}</text>
				</view>
			</view>
		</commonDialog>
	</view>
</template>

<script>
import commonIcon from '@/pagesA/components/common/icon/icon';
import noData from '@/pagesA/components/common/no-data/no-data';
import commonDialog from '@/pagesA/components/common/dialog/dialog';
import { workingStatusList } from '@/pagesA/config/enum';
import { common } from '@/pagesA/utils/common.js';
import moment from 'moment';
const app = getApp();
export default {
	components: {
		noData,
		commonDialog,
		commonIcon
	},
	data() {
		return {
			personList: [],
			orgnationList: [],
			countMap: {
				total: ''
			},
			workingStatusList,
			workingStatus: '',
			selectIndex: 0,
			workTeamUuid: '',
			projectUuid: '',
			type: '',
			orgName: '',
			pageNum: 1,
			pageSize: 20,
			isPullRefresh: false,
			noMore: false,
			loading: true,
			showDialog: false,
			blacklist: {
				name: '',
				recordDate: '',
				reason: ''
			},
			value: '',
			tagIndex: 0,
			tagItem: {
				key: '',
				value: ''
			}
		};
	},
	onLoad(options) {
		this.workingStatus = options.workingStatus || '';
		this.type = options.type;
		this.workingStatusList.unshift({
			code: '',
			value: '全部'
		});
		for (let i = 0; i < this.workingStatusList.length; i++) {
			if (this.workingStatusList[i].code == this.workingStatus) {
				this.selectIndex = i;
				break;
			}
		}
		// this.setData({
		// 	selectIndex: this.selectIndex,
		// 	workingStatus: this.workingStatus,
		// 	workingStatusList: this.workingStatusList,
		// 	orgName: options.orgName
		// });
		this.orgName = options.orgName;
		this.projectUuid = options.projectUuid || '';
		if (this.type != 1) {
			this.workTeamUuid = options.workTeamUuid;
		}
		this.refreshList();
	},
	onShow() {},
	methods: {
		handlePostData(postData) {
			let obj = {};
			for (let key in postData) {
				if (postData[key] || postData[key] === 0) {
					obj[key] = postData[key];
				}
			}
			return obj;
		},

		getOrgnationByParent() {
			//获取非子项目组织机构
			let postData = {
				workTeamUuid: this.workTeamUuid,
				workingStatus: this.workingStatus,
				projectUuid: this.projectUuid
			};
			postData = this.handlePostData(postData);
			common.apis.roster.getOrgnationByParent(postData).then(
				(res) => {
					if (!res.data) {
						return;
					}
					// this.setData({
					// 	orgName: res.data.currentOrgnation?.name || '',
					// 	countMap: res.data.currentOrgnation,
					// 	orgnationList: res.data.orgnationList
					// });
					this.orgName = res.data.currentOrgnation?.name || '';
					this.countMap = res.data.currentOrgnation;
					this.orgnationList = res.data.orgnationList;
					this.listPageRosterByOrgnation();
				},
				() => {
					// this.setData({
					// 	isPullRefresh: false
					// });
					this.isPullRefresh = false;
				}
			);
		},

		getParentOrgnation() {
			//获取子项目组织机构
			let postData = {
				workingStatus: this.workingStatus,
				projectUuid: this.projectUuid,
				calculateModify: 0
			};
			postData = this.handlePostData(postData);
			common.apis.roster.getParentOrgnation(postData).then(
				(res) => {
					if (!res.data) {
						return;
					}
					// this.setData({
					// 	countMap: res.data.countMap,
					// 	orgnationList: res.data.orgnationList,
					// 	isPullRefresh: false
					// });
					this.countMap = res.data.countMap;
					this.orgnationList = res.data.orgnationList;
					this.isPullRefresh = false;
				},
				() => {
					// this.setData({
					// 	isPullRefresh: false
					// });
					this.isPullRefresh = false;
				}
			);
		},

		handleWorkTypeName(item) {
			if (item.personType) {
				if (item.personType == '10') {
					return '管理人员';
				} else if (item.personType == '30') {
					return '班组长';
				}
			}
			return item.workTypeName;
		},

		listPageRosterByOrgnation() {
			if (this.type == 1 || this.noMore) {
				return;
			}
			// this.setData({
			// 	loading: true
			// });
			this.loading = true;
			//查询组织机构下的人员列表
			let postData = {
				pageSize: this.pageSize,
				pageNum: this.pageNum,
				workTeamUuid: this.workTeamUuid,
				workingStatus: this.workingStatus,
				projectUuid: this.projectUuid
			};
			postData = this.handlePostData(postData);
			common.apis.roster.listPageRosterByOrgnation(postData).then(
				(res) => {
					if (!res.data) {
						return;
					}
					let personList = res.data.list;
					for (let item of personList) {
						item.workTypeNameData = this.handleWorkTypeName(item);
						item.avatarData = common.commonMethods.imgShow(item.avatar);
					}
					// this.setData({
					// 	personList: this.personList.concat(personList),
					// 	pageNum: (this.pageNum += 1),
					// 	noMore: this.pageSize > personList.length,
					// 	isPullRefresh: false,
					// 	loading: false
					// });
					this.personList = this.personList.concat(personList);
					this.pageNum = this.pageNum += 1;
					this.noMore = this.pageSize > personList.length;
					this.isPullRefresh = false;
					this.loading = false;
				},
				() => {
					// this.setData({
					// 	isPullRefresh: false,
					// 	noMore: true,
					// 	loading: false
					// });
					this.noMore = true;
					this.isPullRefresh = false;
					this.loading = false;
				}
			);
		},

		changeWorkStatus(e) {
			let index = e.detail.value;
			// this.setData({
			// 	selectIndex: index,
			// 	workingStatus: this.workingStatusList[index].code
			// });
			this.selectIndex = index;
			this.workingStatus = this.workingStatusList[index].code;
			this.refreshList();
		},

		refreshList() {
			// this.setData({
			// 	pageNum: 1,
			// 	isPullRefresh: false,
			// 	orgnationList: [],
			// 	personList: [],
			// 	noMore: false
			// });
			this.pageNum = 1;
			this.isPullRefresh = true;
			this.$nextTick(() => {
				this.isPullRefresh = false;
			});
			this.orgnationList = [];
			this.personList = [];
			this.noMore = false;
			if (this.type == 1) {
				//子项目
				this.getParentOrgnation();
			} else {
				this.getOrgnationByParent();
			}
		},

		showBlackReason(e) {
			const item = e.currentTarget.dataset.item;
			this.blacklist = {
				name: item.name,
				recordDate: item.blacklist?.recordDate ? moment(item.blacklist.recordDate).format('YYYY-MM-DD') : '',
				reason: item.blacklist?.reason || ''
			};
			// this.setData({
			// 	blacklist: this.blacklist,
			// 	showDialog: true
			// });
			this.showDialog = true;
		},
		closeDialog() {
			// this.setData({
			// 	showDialog: false
			// });
			this.showDialog = false;
		},
		toRosterOrg(e) {
			const item = e.currentTarget.dataset.item;
			uni.navigateTo({
				url: `/pagesA/pages/roster/rosterOrg/rosterOrg?projectUuid=${this.projectUuid}&workingStatus=${this.workingStatus}&type=${item.type}&orgName=${item.name}&workTeamUuid=${item.uuid}`
			});
		},
		toPersonDetail(e) {
			const item = e.currentTarget.dataset.item;
			if (item.nationality.code != 'CHN') {
				common.message.toast('外籍人员信息请在系统App或Web端查看');
				return;
			}
			uni.navigateTo({
				url: `/pagesA/pages/roster/detail/detail?uuid=${item.uuid}&projectUuid=${item.projectUuid}`
			});
		}
	}
};
</script>
<style>
.roster-org-container {
	height: 100vh;
	background-color: #f6f7fa;
	display: flex;
	flex-direction: column;
}

.top-filter-container {
	display: flex;
	align-items: center;
	background-color: #fff;
	padding: 16rpx 32rpx 8rpx;
}

.top-filter-container .org-info {
	flex: 1;
	font-size: 28rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #949dbf;
	display: flex;
	align-items: center;
	overflow: hidden;
}

.org-info .org-name {
	margin-right: 16rpx;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.org-info .org-num {
	flex-shrink: 0;
}

.top-filter-container .state-container {
	width: fit-content;
	flex-shrink: 0;
	margin-left: 12rpx;
}

.state-container .state {
	display: flex;
	align-items: center;
	font-size: 28rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #4b5680;
}

.state .down-arrow {
	width: 15rpx;
	height: 15rpx;
	border-top: 4rpx solid #949dbe;
	border-right: 4rpx solid #949dbe;
	transform: rotate(135deg);
	border-radius: 4rpx;
	margin-left: 12rpx;
	position: relative;
	top: -4rpx;
}

.org-list {
	background-color: #fff;
}

.sp .project-item-container:last-child .projext-item {
	border-bottom: 0;
}

.no-data {
	flex: 1;
}

.project-item-container,
.person-item-container {
	padding-left: 32rpx;
	background-color: #fff;
}

.projext-item,
.person-item {
	display: flex;
	border-bottom: 1px solid #e1e2e7;
	padding-top: 32rpx;
	padding-right: 32rpx;
	padding-bottom: 32rpx;
}

.person-item-container:last-child .person-item {
	border-bottom: 0;
}

.project-item-container .project-item-middle,
.person-item-container .person-item-middle {
	flex: 1;
	margin-left: 24rpx;
	overflow: hidden;
}

.project-item-middle .middle-top,
.person-item-middle .middle-top {
	display: flex;
	align-items: center;
	font-size: 34rpx;
	font-family: PingFangSC-Medium, PingFang SC;
	font-weight: 550;
	color: #1e253f;
}

.middle-top .project-type {
	width: 38rpx;
	height: 38rpx;
	background-color: #ff9542;
	border-radius: 50%;
	font-size: 26rpx;
	padding: 4rpx;
	color: #fff;
	text-align: center;
	font-weight: 400;
	margin-right: 6rpx;
	flex-shrink: 0;
}

.middle-top .project-name {
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.project-item-middle .middle-bottom {
	display: flex;
	align-items: center;
	flex-wrap: wrap;
}

.middle-bottom .project-tag {
	display: flex;
	align-items: center;
	justify-content: center;
	width: fit-content;
	height: 40rpx;
	background: #eff1f6;
	border-radius: 6rpx;
	font-size: 24rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #4b5680;
	padding: 0 20rpx 0 25rpx;
	margin-right: 16rpx;
	margin-top: 16rpx;
	flex-shrink: 0;
}

.project-tag .tag-num {
	word-break: break-all;
	margin-left: 5rpx;
}

.projext-item .right-arrow,
.person-item .right-arrow {
	flex-shrink: 0;
	align-self: center;
	width: 15rpx;
	height: 15rpx;
	border-top: 4rpx solid #949dbe;
	border-right: 4rpx solid #949dbe;
	transform: rotate(45deg);
	border-radius: 4rpx;
	margin-left: 32rpx;
}

.icon-container {
	width: 82rpx;
	height: 82rpx;
	border-radius: 16rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.icon-container.green {
	background-color: #00c467;
}

.icon-container.orange {
	background-color: #ff9544;
}

.person-item-middle .middle-top {
	display: flex;
	align-items: center;
}

.person-item-middle .person-name {
	word-break: break-all;
	text-align: justify;
}

.person-item-middle .work-type {
	flex-shrink: 0;
	flex-wrap: wrap;
	max-width: 400rpx;
	background: #eff1f6;
	border-radius: 6rpx;
	font-size: 24rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #4b5680;
	padding: 4rpx 16rpx;
	display: flex;
	align-items: center;
	margin-left: 16rpx;
	margin-bottom: 2rpx;
	text-align: justify;
	word-break: break-all;
}

.person-item-middle .middle-bottom {
	font-size: 28rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #949dbf;
	margin-top: 4rpx;
}

.person-item .icon-container {
	position: relative;
}

.icon-container .black-icon {
	position: absolute;
	top: -24rpx;
	right: -12rpx;
}

.icon-container .person-avatar {
	width: 82rpx;
	height: 82rpx;
	border-radius: 16rpx;
}

.tip-content {
	padding: 40rpx 48rpx 8rpx;
	font-size: 28rpx;
	font-family: PingFangSC-Medium, PingFang SC;
	font-weight: 550;
	color: #1e253f;
}

.tip-content .tip-item {
	display: flex;
	margin-bottom: 32rpx;
}

.tip-content .tip-item-label {
	flex-shrink: 0;
}

.tip-content .tip-item-content {
	flex: 1;
	text-align: justify;
}
</style>
